#include "LandscapeSceneNode.h"

mtBEGIN(LandscapeSceneNode)
	mtBASE(ECS::Component);
	//mtMETHOD(setTile);
	mtMETHOD(onStart);
mtEND

LandscapeSceneNode::LandscapeSceneNode()
{

}

LandscapeSceneNode::~LandscapeSceneNode()
{

}

void LandscapeSceneNode::setTile(int x, int y, int gid)
{
	TileInfo& info = _sprites.get(x, y);
	info.gid = gid;
	if (info.sprite != nullptr)
	{
		info.sprite->removeFromParentAndCleanup(true);
		info.sprite->release();
	}

	info.sprite = new cocos2d::CCSprite();
	info.sprite->initWithFile(_tileSprites[gid].c_str());
	info.sprite->setPosition(ccp(64 * x, 64 * y));
	info.sprite->setScale(0.5f);
	_mainNode->addChild(info.sprite);
}

bool LandscapeSceneNode::initWith(LandscapeSceneNodeDef* def)
{
	_mainNode = new cocos2d::CCNode();
	_tileSprites = def->tileSprites;
	_sprites.resize(def->tiles.getLeftX(), def->tiles.getBottomY(), def->tiles.getRightX(), def->tiles.getTopY());

	for (auto it = def->tiles.begin(); it.isValid(); ++it)
	{
		setTile(it.getX(), it.getY(), *it);
	}
	return true;
}

void LandscapeSceneNode::onStart()
{

}

cocos2d::CCNode* LandscapeSceneNode::getNode() const
{
	return _mainNode;
}
